import 'package:base_flutter/base_flutter.dart';
import 'package:ls_work/net/api/http_api.dart';
import 'package:ls_work/net/post_request.dart';
import 'package:ls_work/net/response/office_list_tree_data.dart';

import '../net/response/office_info_data.dart';

class OfficeModel extends BaseMvvmModel {
  @override
  void onCleared() {}

  ///部门信息
  Future<OfficeInfoData?> infoData(String id) async {
    var map = {"id": id};
    var data = await HttpGo.instance.postData(HttpApi.officeInfo,
        data: map, options: PostRequest.urlencodedHeader);
    if ("0" == data['code']) {
      var list = data['data']['rows'] as List?;
      if (list == null || list.isEmpty) {
        return null;
      } else {
        return OfficeInfoData.fromJson(list[0]);
      }
    } else {
      throw data['msg'];
    }
  }

  ///部门树状列表
  Future<List<OfficeListTreeData>> officeListTree() async {
    var data = await HttpGo.instance.postData(HttpApi.officeListTree);
    if ("0" == data['code']) {
      return ((data['data']['rows'] as List) ?? [])
          .map((e) => OfficeListTreeData.fromJson(e))
          .toList();
    } else {
      throw data['msg'];
    }
  }
}
